package TUILayer;
import java.util.Scanner;
import ControllerLayer.*;
 
public class CustomerUI
{   

    private CustomerController customerController;
    private String idAssistant;
    
    public CustomerUI(String idAssistant)
    {
        this.idAssistant = idAssistant;
        customerController= CustomerController.getInstance();
    }
    
    public void start()
    {
        
        customerMenu();
    }
    
    private String getIDAssistant(){
        return idAssistant;
    }
    
    private void customerMenu()
    {
       boolean exit = false;
       while(!exit)
       {
          int choise = writeCustomerMenu(); 
          if(choise == 1)
          {
              getPerson();
          }
          else{
              if(choise == 2){
                   addPerson();
              }  
              else{
                  if(choise == 3){
                      deletePerson();
                  }  
                    else{
                          if(choise == 4){
                              listAllPersons();
                            }
                          else{
                              exit = true;
                           }
                        }
                    }
                }
       }
    }
    private int writeCustomerMenu()
    {
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** Customer Menu ***");
            System.out.println(" Logged as: " +getIDAssistant());
            System.out.println(" (1) Show information about a customer");
            System.out.println(" (2) Add customer");
            System.out.println(" (3) Delete customer");
            System.out.println(" (4) List all customers");
            System.out.println(" (5) Return");
            System.out.print("\n Make your choice: ");
            
            int choise = keyboard.nextInt();
            
            return choise;
        }
        
    private int inputPersonsID()
    {   
        boolean ok = false;
        int id=0;
        while(!ok)
        {
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Write Id:  ");
           try{
             id = keyboard.nextInt();
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("It should be a number -  try again");
             String input = keyboard.nextLine();
           }
        }
        return id;
    }
    
    private String inputName()
    {
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write Name:  ");
         String name = keyboard.nextLine();
         return name;
    }
   
    private String inputCity()
    {
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write City:  ");
         String city = keyboard.nextLine();
         return city;
    } 
   
    private String  inputAddress()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Write Address: ");
        String address = keyboard.nextLine();
        return address;
    }
    
    private void getPerson()
    {
        int id = inputPersonsID();
        
        try
        {
            System.out.println(customerController.printCustomer(id));
            inputContinue();
        }
        
        catch(NullPointerException exp)
        {
            System.out.println("There isn't person with ID: "+id);
            inputContinue();
        }

    }
    
    private void addPerson()
    {
        int id;
        String name = "";
        String address = ""; 
        
        String city = "";
        
        
        id = inputPersonsID();
        name = inputName();
        address = inputAddress();
        city = inputCity();
     
        
        customerController.createCustomer(id, name, address, city);
        inputContinue();
    }
    
    private void deletePerson()
    {int delete= inputPersonsID();
        try 
        {customerController.deleteCustomer(delete);
        System.out.println("Person has been deleted");
        }
        catch(NullPointerException exp)
        {
            System.out.println("There isn't person with ID: "+delete);
            
        }
        inputContinue();
    }
      
    private void inputContinue()
    {
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Press Enter to continue:  ");
         String code = keyboard.nextLine();
    }
    
    private void listAllPersons()
    {
        System.out.println(customerController.printAllCustomers());
        inputContinue();
    }   
}